package com.example.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.project.entity.Depart;
import com.example.project.entity.Progress;
import com.example.project.entity.Project;
import com.example.project.entity.User;
import com.example.project.enums.ProjectStutusEnum;
import com.example.project.exception.ServiceException;
import com.example.project.mapper.DepartMapper;
import com.example.project.mapper.ProgressMapper;
import com.example.project.mapper.ProjectMapper;
import com.example.project.mapper.UserMapper;
import com.example.project.service.ProjectService;
import com.example.project.service.UserService;
import com.example.project.vo.model.PageDto;
import com.example.project.vo.model.ProUserVo;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;

/**
 * (Project)表服务实现类
 */
@Service("projectService")
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project>
        implements ProjectService {
    @Resource
    private ProjectMapper projectMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private DepartMapper departMapper;

    @Resource
    private ProgressMapper progressMapper;

    @Resource
    private UserService userService;

    @Override
    public Project queryById(Long id) {
        Project project = this.getBaseMapper().selectById(id);
        dealProject(project);
        return project;
    }

    @Override
    public PageInfo<Project> queryAll(PageDto pageDto) {
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());
        List<Project> list = this.getBaseMapper().selectList(pageDto.toQueryWrapper());
        list.forEach(project -> {
            dealProject(project);
        });
        return new PageInfo<>(list);
    }

    @Override
    public Project insert(Project project) {
        project.setProjStatus(ProjectStutusEnum.DOING);
        project.setApplyStatus(1);
        this.getBaseMapper().insert(project);
        return project;
    }

    @Override
    public Project update(Project project) {
        this.getBaseMapper().updateById(project);
        return this.queryById(project.getId());
    }

    @Override
    public List<Project> querryList() {
        return this.getBaseMapper().selectList(null);
    }

    @Override
    public boolean deleteById(Long id) {
        return this.getBaseMapper().deleteById(id) > 0;
    }

    @Override
    public void updateStatus(Long id, Integer status) {

        if (  status == 1) {
            Integer percentage = progressMapper.getPercentage(id);
            if( ObjectUtils.isEmpty(percentage) || percentage < 100 ){
                throw new ServiceException("项目进度未达到100%!");
            }
        }
        this.getBaseMapper().update(null, new LambdaUpdateWrapper<Project>()
                .set(Project::getProjStatus, status)
                .eq(Project::getId, id));
    }

    @Override
    public void review(Long id, Integer applyStatus) {
        this.getBaseMapper().update(null, new LambdaUpdateWrapper<Project>()
                .set(Project::getApplyStatus, applyStatus)
                .eq(Project::getId, id));
    }

    private void dealProject(Project project) {
        Long projUserId = project.getProjUserId();
        Long proId = project.getId();
        if (ObjectUtils.isNotEmpty(projUserId)) {

            User user = userMapper.selectById(projUserId);

            // 处理 项目负责人名
            project.setProjUserName(user.getUserName());

            // 处理 部门
            Long depId = user.getDepId();
            Depart depart = departMapper.selectById(depId);
            project.setDepart(depart);

            // 执行进度
            Integer percentage = progressMapper.getPercentage(proId);
            project.setPercentage(ObjectUtils.isEmpty(percentage) ? 0 : percentage);

            // 处理关联人
            List<User> users = userService.proUserList(proId);
            project.setUserList(users);

        }
    }


}
